


alter      procedure p_makeredvou
  @voucherid integer,
  @opid integer,
  @filid integer,
  @returncode integer out
as   
  declare  @newvoucherid integer
  ,  @binder_by integer
  ,  @vouwordid integer
  ,  @newvouno integer
  ,  @neworderno integer
  ,  @curryear integer
  ,  @currmonth integer
  ,  @vouyear integer
  ,  @voumonth integer
  ,  @vouday integer
  ,  @newfilorderno integer
  ,  @today datetime
  ,  @makebcode nvarchar(60)
  ,  @str nvarchar(1000)
begin
  set nocount on
  select @binder_by = binder_by, @vouwordid= vouwordid from tfb_voucher where voucherid = @voucherid
  if (@binder_by is null) 
  begin
    set @returncode = -1
    return;
  end

  select  @vouyear=year(bdate),@voumonth=month(bdate),@vouday=day(bdate) from tfb_voucher where  voucherid = @voucherid
  select  @curryear=p_paramvalue from ts_parameters where p_paramnm='FinanceAccYear'
  select  @currmonth=p_paramvalue from ts_parameters where p_paramnm='FinanceAccMonth'

  if((@vouyear<@curryear) or (@vouyear=@curryear and @voumonth<@currmonth))
  begin
   select @today =edate from tfd_monthlycalendar  where yearid=@curryear and monthid=@currmonth
  end
  else
  begin
    select @today=edate from tfd_monthlycalendar  where yearid=@vouyear and monthid=@voumonth
  end
  
  execute p_fingetnewvouorderno @today,@filid,@vouwordid,@newvouno out, @neworderno out,@newfilorderno out
--  execute p_getnewkey 'tfb_voucher',@newvoucherid out 
  set @str=N'exec p_getnewbidcode @targettab,@filid,@btype,@bdate,@mkbid output,@mkbcode output'
  execute sp_executesql 
          @str,
          N' @targettab nvarchar(30), @filid int,@btype int,@bdate datetime,@mkbid int output,@mkbcode nvarchar(60) output',
          @targettab = N'tfb_voucher',
          @filid=@filid,
          @btype=25010,
          @bdate=@today,
          @mkbid=@newvoucherid output,
          @mkbcode=@makebcode output

  insert into tfb_voucher(voucherid, create_by, signer_by, vouwordid, orderno, voucherno, 
      bcode, bdate, update_time,formerbno, formertype, formerbid, 
      vouyearmonth, check_by, binder_by,filid,filorderno)
  select @newvoucherid, @opid, null, vouwordid, @neworderno, @newvouno, 
      @makebcode, @today, @today, 0, 0, null, 
       '', null, null,@filid,@newfilorderno
  from tfb_voucher where voucherid = @voucherid
  
  insert into tfb_voucherdtl (voucherid, bno, deptid, projectid, balanceid, 
                           subjectid, traderid, productid, empid, borderno, explan, 
                           dodebit, dcdebitamt, dccreditamt, exchrate, debitamt, creditamt, 
                           quantity, price, baldate, balbcode,
                           unclearamt, readonly, baled)
  select @newvoucherid, bno, deptid, projectid, balanceid, subjectid, traderid, productid,
         empid, borderno, explan, dodebit, -dcdebitamt, -dccreditamt, exchrate, -debitamt, -creditamt, 
         -quantity, price, baldate, balbcode,
         unclearamt, readonly, null
  from tfb_voucherdtl
  where voucherid = @voucherid
  
  set @returncode = @newvoucherid
  
end





GO
